package com.wkbb.sports.controller;

import com.github.pagehelper.PageInfo;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.sports.dto.TermDto;
import com.wkbb.common.utils.StringUtil;
import com.wkbb.sports.service.TermService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(description = "考试管理")
@RequestMapping("/sports/{apiVersion}/term")
@Slf4j
public class TermController {

    @Autowired
    private TermService termService;

    @ApiOperation(value = "查询考试管理分页")
    @RequestMapping(value = "getTermPageList", method = RequestMethod.GET)
    public ResultDTO<PageInfo<TermDto>> getTermPageList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "考试ID", required = false) @RequestParam(name = "termId", required = false) String termId,
            @ApiParam(value = "考试时间", required = false) @RequestParam(name = "termTime", required = false) String  termTime,
            @ApiParam(value = "页码", required = true, defaultValue = "1") @RequestParam(name = "pageNo", required = true, defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页条数", required = true, defaultValue = "10") @RequestParam(name = "pageSize", required = true, defaultValue = "10") Integer pageSize) {
        log.info("getExaminePageList orgId:{} termId:{}  termTime{} pageNo:{} pageSize:{}", orgId, termId,termTime,pageNo, pageSize);
        try {
            if (orgId == null || orgId.equals("")){
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("orgId", orgId);
            params.put("termId", termId);
            if (!StringUtil.isEmpty(termTime)){
                params.put("termTime",termTime);
            }
            return new ResultDTO<>(termService.getTermPageList(params, pageNo, pageSize));
        } catch (Exception e) {
            log.error("***********TermController.getExaminePageList******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "新增考试")
    @RequestMapping(value = "/addTerm", method = RequestMethod.POST)
    public ResultDTO addTerm(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @RequestBody TermDto termDto) {
        log.info("getExaminePageList termData{}", termDto.toString());
        try {
            if (termDto.getOrgId() == null){
                return new ResultDTO(ErrorCode.PARAM_MISSING);
            }
            return termService.addTerm(termDto);
        } catch (Exception e) {
            log.error("***********TermController.addTerm******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "删除考试")
    @RequestMapping(value = "/delTerm", method = RequestMethod.DELETE)
    public ResultDTO delTerm(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                             @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
                             @ApiParam(value = "考试ID", required = true) @RequestParam(name = "termId", required = true) Long termId){
        log.info("delTerm orgId{}  termId{}", orgId,termId);
        try {
            if (orgId == null || termId==null){
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            return termService.delTerm(orgId,termId);
        }catch (Exception e){
            log.error("***********TermController.delTerm******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "考试项目类型列表 -- 国家体测")
    @RequestMapping(value = "/getTermType", method = RequestMethod.GET)
    public ResultDTO getTermType(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                 @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId) {
        log.info("delTerm orgId{} ", orgId );
        try {
            if (orgId == null ){
                return new ResultDTO(ErrorCode.PARAM_MISSING);
            }
            List<TermDto> termDtos = termService.getTermType(orgId);
            return new ResultDTO(termDtos);
        }catch (Exception e){
            log.error("***********TermController.getTermType******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "考试列表详情")
    @RequestMapping(value = "/getTerm", method = RequestMethod.GET)
    public ResultDTO getTerm(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                             @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
                             @ApiParam(value = "考试id", required = true) @RequestParam(name = "termId", required = true) Long termId){
        try {
            if (orgId == null || termId==null){
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            Map<String,Object> params = new HashMap<>();
            params.put("orgId",orgId);
            params.put("termId",termId);
            return termService.getTerm(params);
        }catch (Exception e){
            e.printStackTrace();
            log.error("***********TermController.getTerm******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "修改考试")
    @RequestMapping(value = "/updateTerm", method = RequestMethod.PUT)
    public ResultDTO updateTerm(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                @RequestBody TermDto termDto){
        try {
            if (termDto.getOrgId() == null|| termDto.getId() == null){
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            return termService.updateTerm(termDto);
        }catch (Exception e){
            e.printStackTrace();
            log.error("***********TermController.updateTerm******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "修改考试状态")
    @RequestMapping(value = "/updateTermStatus", method = RequestMethod.PUT)
    public ResultDTO updateTermStatus(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion)
    {
        try {
            termService.updateTermStatus();
            return new ResultDTO<>(true);
        }catch (Exception e){
            e.printStackTrace();
            log.error("***********TermController.updateTermStatus******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }
}